package com.ideaplugin.datamanager.common;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;


/**
 * @author fantong
 * Springmvc 拦截器注册
 */
@Configuration
@EnableWebMvc
//public class InterceptorConfig extends WebMvcConfigurerAdapter {
//public class InterceptorConfig extends WebMvcConfigurationSupport {

public class InterceptorConfig implements WebMvcConfigurer {
    //    @Autowired
//    @Resource
//    private LoginInterceptor loginInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        //注册拦截器
        registry.addInterceptor(new HandlerInterceptor() {
        }).excludePathPatterns("/**");
        //registry.addInterceptor(loginInterceptor)
        //        .addPathPatterns("/**")
        //        .excludePathPatterns("/user/XXXXXX/XXXXXX","/XXXXXX/XXXXXX","/user/XXXXXX/XXXXXX","/XXXXXX/v2/alipay_callback", "/XXXXXX/iosPay","/error","/XXXXXX/v2/wxCallBack","/XXXXXX/v2/XXXXXX","/swagger-resources/**","/webjars/**","/static/**","/swagger-ui.html/**");//"/qrcode/v2/pc/inquireLoginState",
//                .excludePathPatterns(String.valueOf(excludePath));
        //在springboot2.0.0之后继承WebMvcConfigurationSupport类，重写addInterceptors方法
        //     *
//        super.addInterceptors(registry);
    }

    @Bean
    public InternalResourceViewResolver defaultViewResolver() {
        return new InternalResourceViewResolver();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .addResourceLocations("classpath:/resources/");
//        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
//
//        registry.addResourceHandler("swagger-ui.html", "doc.html").addResourceLocations("classpath:/META-INF/resources/");
//        //Windows下
//        registry.addResourceHandler("/uploads2/**").addResourceLocations("file:D:/springbootFile/upload/images/");
//        //Mac或Linux下(没有CDEF盘符)
//        registry.addResourceHandler("/uploads/**").addResourceLocations("file:/home/qmake/workspace/images/userImg/");
////        super.addResourceHandlers(registry);


    }


    /**
     * 跨域支持
     *
     * @author QianChi
     * @date 2021/6/30 9:55
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
//                .allowedOrigins("*")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS", "HEAD")
                .maxAge(3600 * 24);
    }
}